package com.sanchunhui.psc.widgets

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.view.View
import android.util.AttributeSet
import com.sanchunhui.psc.R


/**
 * @Author zyx
 * @Date 2021/8/19-9:16
 * @Dscribe
 */
open class BatteryView(context: Context,attrs: AttributeSet?=null) : View(context,attrs) {
    private var mPower = 100
    private var orientation: Int = 0
    private var widths: Int = 0
    private var heights: Int = 0
    private var mColor: Int = 0

    init {
        var typedArray : TypedArray = context.obtainStyledAttributes(attrs, R.styleable.Battery)
        mColor = typedArray.getColor(R.styleable.Battery_batteryColor, 0xFFFFFFFF.toInt())
        orientation = typedArray.getInt(R.styleable.Battery_batteryOrientation, 0)
        mPower = typedArray.getInt(R.styleable.Battery_batteryPower, 100)
        widths = getMeasuredWidth()
        heights = getMeasuredHeight()
        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //对View上的內容进行测量后得到的View內容占据的宽度
        widths = getMeasuredWidth()
        //对View上的內容进行测量后得到的View內容占据的高度
        heights = getMeasuredHeight()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //判断电池方向    horizontal: 0   vertical: 1
        if (orientation == 0) {
            drawHorizontalBattery(canvas!!);
        } else {
            drawVerticalBattery(canvas!!);
        }
    }

    /**
     * 绘制水平电池
     *
     * @param canvas
     */
    fun drawHorizontalBattery(canvas:Canvas ) {
        var paint : Paint = Paint()
        paint.setColor(mColor)
        paint.setStyle(Paint.Style.STROKE)
        var strokeWidth : Float  = (widths / 20).toFloat()
        var strokeWidth_2 : Float  = strokeWidth / 2
        paint.setStrokeWidth(strokeWidth)
        var r1 : RectF = RectF(strokeWidth_2, strokeWidth_2, widths - strokeWidth - strokeWidth_2, height - strokeWidth_2)
        // 设置外边框颜色为黑色
        paint.setColor(Color.BLACK)
        canvas.drawRect(r1, paint)
        paint.setStrokeWidth(0F)
        paint.setStyle(Paint.Style.FILL)
        //画电池内矩形电量
        var offset : Float= (widths - strokeWidth * 2) * mPower / 100
        var r2 : RectF = RectF(strokeWidth, strokeWidth, offset, heights - strokeWidth)
        //根据电池电量决定电池内矩形电量颜色
        if (mPower < 30) {
            paint.setColor(Color.RED)
        }
        if (mPower >= 30 && mPower < 50) {
            paint.setColor(Color.BLUE)
        }
        if (mPower >= 50) {
            paint.setColor(Color.GREEN)
        }
        canvas.drawRect(r2, paint)
        //画电池头
        var r3 : RectF = RectF(widths - strokeWidth, heights * 0.25f, widths.toFloat(), heights * 0.75f)
        //设置电池头颜色为黑色
        paint.setColor(Color.BLACK)
        canvas.drawRect(r3, paint)
    }

    /**
     * 绘制垂直电池
     *
     * @param canvas
     */
    fun drawVerticalBattery(canvas :Canvas) {
        var paint : Paint = Paint()
        paint.setColor(mColor)
        paint.setStyle(Paint.Style.STROKE)
        var strokeWidth = height / 20.0f
        var strokeWidth2 = strokeWidth / 2
        paint.setStrokeWidth(strokeWidth);
        var headHeight =  (strokeWidth + 0.5f)
        var rect : RectF = RectF(strokeWidth2, headHeight + strokeWidth2, width - strokeWidth2, height - strokeWidth2)
        canvas.drawRect(rect, paint);
        paint.setStrokeWidth(0F);
        var topOffset = (height - headHeight - strokeWidth) * (100 - mPower) / 100.0f
        var rect2 : RectF= RectF(strokeWidth, headHeight + strokeWidth + topOffset, width - strokeWidth, height - strokeWidth)
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(rect2, paint);
        var headRect : RectF = RectF(widths / 4.0f, 0F, widths * 0.75f, headHeight)
        canvas.drawRect(headRect, paint);
    }

    /**
     * 设置电池电量
     *
     * @param power
     */
     fun setPower(power:Int) {
        this.mPower = power
        if (mPower < 0) {
            mPower = 100
        }
        invalidate()//刷新VIEW
    }

    /**
     * 设置电池颜色
     *
     * @param color
     */
    fun setColor( color: Int) {
        this.mColor = color
        invalidate()
    }

    /**
     * 获取电池电量
     *
     * @return
     */
    fun getPower() :Int{
        return mPower
    }




}